//初始化
//1、初始化
$(function () {
    fetchData();

    //点击搜索按钮
    $("#btnSearch").click(function () {
        //重新加载数据
        $('#tb').bootstrapTable("refresh", {
            pageNumber: 1
        });
    });

});

function fetchData() {
    $('#tb').bootstrapTable({
        method: 'get',
        url: '/supplier/search',
        pagination: true,
        pageNumber: 1,
        height: 500,
        pageSize: 2,
        pageList: [2, 10, 20, 30, 50, 100],
        sidePagination: "server",
        queryParamsType: "page",
        queryParams: function (params) {         //自定义查询条件
            params["supplierName"] = $("#supplierName").val();
            params["supplierTel"] = $("#supplierTel").val();
            return params;
        },
        clickToSelect: true,
        columns: [
            {
                title: '供应商编号',
                field: 'supplierId'
            },
            {
                title: '供应商名称',
                field: 'supplierName'
            },
            {
                title: '负责人',
                field: 'supplierPerson'
            },
            {
                title: '联系电话',
                field: 'supplierTel'
            },
            {
                title: '操作',
                field: 'supplierId',
                formatter: optionTemplate
            },
        ],
        responseHandler: function (res) {
            if (res.code === 1) {
                res = res.data;
            }
            return res;
        }
    });
}

function optionTemplate(value, row, index) {
    let str = `<a class="btn btn-primary" onclick='editMethod(${index})' >修改</a>  <a class="btn btn-danger" onclick='deleteMethod(${index})'>删除</a>`;
    return str;
}

function editMethod(index) {
    console.log("编辑");
    let arr = $('#tb').bootstrapTable('getData');
    let data = arr[index];
    console.log(data);
    location.href="/supplier/update?supplierId="+data.supplierId;
    console.log(data);
}
function deleteMethod(index) {
    console.log("删除");
    let arr = $('#tb').bootstrapTable('getData');
    let data = arr[index];

    if (confirm("你确定要删除设个数据吗?")) {
        $.ajax({
            type: "get",
            url: "/supplier/delete",
            data: "id=" + data.supplierId,
            dataType: "json",
            success: function (resp) {
                if (resp.code === 1) {
                    alert(resp.data);
                    $("#btnSearch").trigger("click");
                } else {
                    alert(resp.data);
                }
            }
        });
    }
}